﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using ET;
using System.Web.UI.HtmlControls;

namespace Presentation.Page
{
    public partial class asset_repair_line : System.Web.UI.Page
    {
        #region[Define]

        private DataTable dtAssetRepairLine;
        private DataTable dtAssetRepairLineExport;
        private DataTable dtAssetRepairLine_Detail_Edit;
        private DataTable dtSearch;
        private DataTable dtAutoComplete;
        protected string SuggestionList;
        protected string StrAlert = "";

        private BLL_AssetRepairLine clsAssetRepairLine = new BLL_AssetRepairLine();

        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            Autocomplate();
            DisableTexBox();
            imgSave.Visible = false;
            if (!IsPostBack)
                if (!IsPostBack)
                {
                    BindAssetRepairLine(string.Empty);
                    BindGridAssetRepairExport();
                }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            BindAssetRepairLine(string.Empty);
        }
        protected void cmdDuyet_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        imgSave.Enabled = false;
                        BindGridAssetRepair_Detail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#assetrepairline");
                        upNewUpdatePanel.Update();
                    }
                }
            }

        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {
            
        }
        protected void imgrefesh_Click(object sender, EventArgs e)
        {

        }
        protected void imgSave_Click(object sender, EventArgs e)
        {

        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {

        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        //-------------list-view-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Accept_0" || e.CommandName == "Accept_1" || e.CommandName == "Accept_2" ||
                e.CommandName == "Accept_3" || e.CommandName == "Accept_4" || e.CommandName == "Accept_5" ||
                e.CommandName == "Accept_6" || e.CommandName == "Accept_7")
            {
                string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                BindGridAssetRepair_Detail_Edit(int.Parse(SenderId));
                OpenDialog("#assetrepairline");
                upNewUpdatePanel.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                //LinkButton linkdouble = (LinkButton)e.Item.FindControl("LbDouble");
                //string _jsDB = ClientScript.GetPostBackClientHyperlink(linkdouble, "");
                //linkdouble.Attributes["ondblclick"] = _jsDB;
                //linkdouble.Attributes["onclick"] = "return false;";

                #region[Double-Click]
                Label lbNote = (Label)e.Item.FindControl("Label1");

                LinkButton linkListName_0 = (LinkButton)e.Item.FindControl("linkListName_0");
                string _jsDouble0 = ClientScript.GetPostBackClientHyperlink(linkListName_0, "");
                linkListName_0.Attributes["ondblclick"] = _jsDouble0;
                linkListName_0.Attributes["onclick"] = "return false;";

                LinkButton linkListName_1 = (LinkButton)e.Item.FindControl("linkListName_1");
                LinkButton linkListName_2 = (LinkButton)e.Item.FindControl("linkListName_2");
                LinkButton linkListName_3 = (LinkButton)e.Item.FindControl("linkListName_3");
                LinkButton linkListName_4 = (LinkButton)e.Item.FindControl("linkListName_4");
                LinkButton linkListName_5 = (LinkButton)e.Item.FindControl("linkListName_5");
                LinkButton linkListName_6 = (LinkButton)e.Item.FindControl("linkListName_6");
                LinkButton linkListName_7 = (LinkButton)e.Item.FindControl("linkListName_7");

                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName_1, "");
                string _jsDouble2 = ClientScript.GetPostBackClientHyperlink(linkListName_2, "");
                string _jsDouble3 = ClientScript.GetPostBackClientHyperlink(linkListName_3, "");
                string _jsDouble4 = ClientScript.GetPostBackClientHyperlink(linkListName_4, "");
                string _jsDouble5 = ClientScript.GetPostBackClientHyperlink(linkListName_5, "");
                string _jsDouble6 = ClientScript.GetPostBackClientHyperlink(linkListName_6, "");
                string _jsDouble7 = ClientScript.GetPostBackClientHyperlink(linkListName_7, "");

                linkListName_1.Attributes["ondblclick"] = _jsDouble1;
                linkListName_1.Attributes["onclick"] = "return false;";

                linkListName_2.Attributes["ondblclick"] = _jsDouble2;
                linkListName_2.Attributes["onclick"] = "return false;";

                linkListName_3.Attributes["ondblclick"] = _jsDouble3;
                linkListName_3.Attributes["onclick"] = "return false;";

                linkListName_4.Attributes["ondblclick"] = _jsDouble4;
                linkListName_4.Attributes["onclick"] = "return false;";
                //------------------------
                linkListName_5.Attributes["ondblclick"] = _jsDouble5;
                linkListName_5.Attributes["onclick"] = "return false;";

                linkListName_6.Attributes["ondblclick"] = _jsDouble6;
                linkListName_6.Attributes["onclick"] = "return false;";

                linkListName_7.Attributes["ondblclick"] = _jsDouble7;
                linkListName_7.Attributes["onclick"] = "return false;";

                #endregion

                #region[Char]
                if (linkListName_1.Text.Length > 14)
                {
                    linkListName_1.Text = linkListName_1.Text.Substring(0, 10).ToString() + "...";
                }
                if (linkListName_2.Text.Length >= 14)
                {
                    linkListName_2.Text = linkListName_2.Text;
                }

                if (linkListName_3.Text.Length >= 14)
                {
                    linkListName_3.Text = linkListName_3.Text.Substring(0, 14).ToString() + "...";
                }
                if (linkListName_4.Text.Length >= 14)
                {
                    linkListName_4.Text = linkListName_4.Text;
                }

                if (linkListName_5.Text.Length >= 14)
                {
                    linkListName_5.Text = linkListName_5.Text.Substring(0, 14).ToString() + "...";
                }
                if (linkListName_6.Text.Length >= 14)
                {
                    linkListName_6.Text = linkListName_6.Text;
                }
                #endregion
            }
        }
        protected void ListViewExport_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            
        }
        protected void ListViewAll_Sorting(Object sender, ListViewSortEventArgs e)
        {
            string SortDirection = "ASC";
            if (ViewState["SortExpression"] != null)
            {
                if (ViewState["SortExpression"].ToString() == e.SortExpression)
                {
                    ViewState["SortExpression"] = null;
                    SortDirection = "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = e.SortExpression;
                }
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
            }
            string sortExpression = e.SortExpression + " " + SortDirection;
            BindAssetRepairLine(sortExpression);
        }

        protected void ListViewAll_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPagerListAll.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            BindAssetRepairLine(string.Empty);
        }
        #endregion

        #region [Function]
        protected void BindAssetRepairLine(string sortExpression)
        {
            string a = ListViewAll.SortExpression.ToString();
            dtAssetRepairLine = clsAssetRepairLine.BindGirdAssetRepairLine();
            if (txtSearch.Text == "")
            {
                if (dtAssetRepairLine != null && dtAssetRepairLine.Rows.Count > 0)
                {
                    dtAssetRepairLine.DefaultView.Sort = sortExpression;
                    ListViewAll.DataSource = dtAssetRepairLine;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
                else
                {
                    ListViewAll.Visible = false;
                    DataPagerListAll.Visible = false;
                }
            }
            else
            {
                if (dtSearch != null && dtSearch.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtSearch;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
            }
        }
        protected void BindGridAssetRepairExport()
        {
            dtSearch = clsAssetRepairLine.Search(txtSearch.Text);
            if (txtSearch.Text == "")
            {
                dtAssetRepairLineExport = clsAssetRepairLine.BindGirdAssetRepairLine();
                if (dtAssetRepairLineExport != null && dtAssetRepairLineExport.Rows.Count > 0)
                {
                    ListViewExport.DataSource = dtAssetRepairLineExport;
                    ListViewExport.DataBind();
                    ListViewExport.Visible = true;
                }
                else
                {
                    ListViewExport.Visible = false;
                }
            }
            else
            {
                if (dtSearch != null && dtSearch.Rows.Count > 0)
                {
                    ListViewExport.DataSource = dtSearch;
                    ListViewExport.DataBind();
                    ListViewExport.Visible = true;
                }
            }
        }
        protected void BindGridAssetRepair_Detail_Edit(int id)
        {
            dtAssetRepairLine_Detail_Edit = clsAssetRepairLine.BindGirdAssetRepairLine_Detail(id);
            if (dtAssetRepairLine_Detail_Edit != null && dtAssetRepairLine_Detail_Edit.Rows.Count > 0)
            {
                lbID.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["STT"].ToString();
                lbIDXoa.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["STT"].ToString();
                txtMaPhuTung.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["Mã Phụ Tùng"].ToString();
                txtTenPhuTung.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["Tên Phụ Tùng"].ToString();
                txtSoLuong.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["Số Lượng"].ToString();
                txtGiaTriHienTai.Text = String.Format("{0:0,00}", dtAssetRepairLine_Detail_Edit.Rows[0]["GIÁ TRỊ HIỆN TẠI"]).ToString().Replace(".", ",");
                txtHangSanXuat.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["Hãng Sản Xuất"].ToString();
                txtGhiChu.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["Ghi Chú"].ToString();
                txtHanBaoHanh.Text = dtAssetRepairLine_Detail_Edit.Rows[0]["Hạn Bảo Hành"].ToString();
            }
        } 
        protected void Autocomplate()
        {
            dtAutoComplete = clsAssetRepairLine.BindGirdAssetRepairLine();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["Mã Phụ Tùng"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Tên Phụ Tùng"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Tài Sản"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Hãng Sản Xuất"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["Mã Phụ Tùng"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Tên Phụ Tùng"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Tài Sản"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Hãng Sản Xuất"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsAssetRepairLine.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();

                ListViewExport.DataSource = dtSearch;
                ListViewExport.DataBind();
                ListViewExport.Visible = true;

                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;

                //.Visible = true;

                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        #endregion

        #region[Texbox]
        protected void DisableTexBox()
        {
            txtMaPhuTung.ReadOnly = true;
            txtTenPhuTung.ReadOnly = true;
            txtSoLuong.ReadOnly = true;
            txtTaiSan.ReadOnly = true;
            txtGiaTriHienTai.ReadOnly = true;
            txtHangSanXuat.ReadOnly = true;
            txtGhiChu.ReadOnly = true;
            txtHanBaoHanh.ReadOnly = true;
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

    }
}